<?php include "header.php"; ?>
<?php
if (isset($_POST["geofence_id"]) && isset($_POST["title"]) && isset($_POST["message"]) && isset($_POST["url"]) && isset($_POST["image_url"])) {
    $geofence_id = $_POST["geofence_id"];
    $title = $_POST["title"];
    $message = $_POST["message"];
    $url = $_POST["url"];
    $image_url = $_POST["image_url"];
    // Store user details in db
   
    // include_once './GCM.php';

    $db = new DB_Functions();
    //$gcm = new GCM();
   // $titik = convert($path);
    $res = $db->insertIklan($geofence_id, $title, $message, $url, $image_url);

    // $registatoin_ids = array($gcm_regid);
    // $message = array("product" => "shirt");
    // $result = $gcm->send_notification($registatoin_ids, $message);
    //echo $result;
    echo "Added row.<br />";
    echo "<a href='view_iklan.php'>Back To Listing</a>";
} else {
    //echo "error";
    // user details missing
}
?>

<?php
include_once './GCM.php';
?>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script type="text/javascript">
var map;
$(document).ready(function() {
    map = new GMaps({
        el: '#map',
        lat: - 12.043333,
        lng: - 77.028333
    });

    <?php
    $db = new DB_Functions();
    $query = $db->getAllGeo();
    if ($query != false) {
        $num_row = mysql_num_rows($query);
    } else {
        $num_row = 0;
    }



    $i = 1;

    if ($num_row > 0) {
        while ($poly = mysql_fetch_array($query)) {




            echo "var path = [". $poly['path'] ."];". $poly['nama_toko'] . " = map.drawPolygon({
                paths: path,
                strokeColor: '" . $poly['warna'] . "',
                strokeOpacity: 1,
                strokeWeight: 3,
                fillColor: '" . $poly['warna'] . "',
                fillOpacity: 0.6});";


$i++;
// }
}
} else {
    echo "error";
}


$db3 = new DB_Functions();
$query3 = $db3->getAllUsers();
if ($query3 != false) {
    $num_row3 = mysql_num_rows($query3);
} else {
    $num_row3 = 0;
}
if ($num_row3 > 0) {
    ?>
<?php
while ($row = mysql_fetch_array($query3)) {

echo "marker".$row['id'] . " = map.addMarker({
    lat: -12.043333,
    lng: -77.028333,
    draggable: true,
});
google.maps.event.addListener(marker" . $row['id'] . ", 'dragend', function() {
            //   updateMarkerStatus('Drag ended');
cek(marker" . $row['id'] . ".getPosition(),'".$row['gcm_regid']."');
document.getElementById(\"posisi\").innerHTML =marker" . $row['id'] . ".getPosition();

});";      
}
} else {


} 


echo"       function cek(latLng,regId) {";
//document.getElementById('info').innerHTML = [latLng.lat(),latLng.lng()].join(', ');
$db2 = new DB_Functions();
$query2 = $db2 -> getAllGeo();
if ($query2 != false) {
$num_row2 = mysql_num_rows($query2);
} else {
$num_row2 = 0;
}
if ($num_row2 > 0) {
                                //for ($count2 = 0; $count2 <= $num_row2; $count2++) {
while ($poly2 = mysql_fetch_array($query2)) {
echo " if (map.checkGeofence(parseFloat(latLng.lat()), parseFloat(latLng.lng()), ".$poly2['nama_toko'].")) {
    alert(\"".$poly2['nama_toko']."\"); loadXMLDoc(regId,'".$poly2['id']."') ";
                                                // $gcm->send_notification($registatoin_ids, $message);
    echo "}";


                                                // $i++;
}
                                        // }
} else {
echo "error";
}
echo "}";
?>
function loadXMLDoc(id, message)
{
var xmlhttp;
if (window.XMLHttpRequest)
                                                {// code for IE7+, Firefox, Chrome, Opera, Safari
                                                    xmlhttp = new XMLHttpRequest();
                                                }
                                                else
                                                {// code for IE6, IE5
                                                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                                                }
                                                xmlhttp.onreadystatechange = function()
                                                {
                                                    if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
                                                    {
                                                        document.getElementById("status").innerHTML = xmlhttp.responseText;
                                                    }
                                                }
                                                xmlhttp.open("GET", "send_message.php?regId=" + id + "&id=" + message, true);
                                                xmlhttp.send();
                                            }
                                        });
</script>
<div class="row-fluid sortable">
    <div class="box span9">
        <div id="map"></div>
    </div>
    <div class="box span3">
        <div class="box-content">
            <form action="insert_iklan.php" method="POST">
                <div class="control-group">
                    <label class="control-label">Geofence Id :</label>
                    <div class="controls">
                        <input type="text" class="input-xlarge" name="geofence_id">
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label">Tittle :</label>
                    <div class="controls">
                        <input type="text" class="input-xlarge" name="tittle">
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label">Message :</label>
                    <div class="controls">
                        <input type="text" class="input-xlarge" name="message">
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label">Url </label>
                    <div class="controls">
                        <input type="text" class="input-xlarge" name="url">
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label">Image Url :</label>
                    <div class="controls">
                        <input type="text" class="input-xlarge" name="image_url">
                    </div>
                </div>

                <input class="btn btn-primary" type="submit">
            </form>
        </div>
    </div>
</div><!--/row-->
<?php include "footer.php"; ?>
